package com.sddfp.system.listener;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.sddfp.common.constant.mq.MQConstant;
import com.sddfp.common.pojo.BankCardRequestDto;
import com.sddfp.system.entity.UserCard;
import com.sddfp.system.entity.UserDetail;
import com.sddfp.system.service.IUserCardService;
import com.sddfp.system.service.IUserDetailService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@Slf4j
@Component
public class BindCardListener {

    private final IUserDetailService userDetailService;
    private final IUserCardService userCardService;
    public BindCardListener(IUserDetailService userDetailService, IUserCardService userCardService) {
        this.userDetailService = userDetailService;
        this.userCardService = userCardService;
    }

    /**
     * 监听指定队列中的消息
     * @Payload
     * 1、实现把消息的json格式转换为对象
     * 2、注意：需要配置消息转换器
     */
    @RabbitListener(queues = MQConstant.SDDFP_QUEUE_BIND)
//    @RabbitHandler
    public void handlerMsg(@Payload BankCardRequestDto bankCardRequestDto) {


//        // 创建固定大小线程池
//        ExecutorService executor = Executors.newFixedThreadPool(2);
//
//        try {
//            // 任务1：更新用户表绑卡状态
//            CompletableFuture<Void> task1 = CompletableFuture.runAsync(() -> {
//                userDetailService.update(Wrappers.lambdaUpdate(UserDetail.class)
//                        .set(UserDetail::isCardBindStatus, true)
//                        .eq(UserDetail::getReqCode, bankCardRequestDto.getReqCode())
//                );
//            }, executor);
//
//            // 任务2：查询用户ID
//            CompletableFuture<UserDetail> task2 = CompletableFuture.supplyAsync(() -> {
//                return userDetailService.getOne(
//                        Wrappers.lambdaQuery(UserDetail.class)
//                                .select(UserDetail::getId)
//                                .eq(UserDetail::getUserCode, bankCardRequestDto.getUserCode())
//                );
//            }, executor);
//
//            // 任务3：等待用户ID查询完成后更新绑卡记录表
//            CompletableFuture<Void> task3 = task2.thenAcceptAsync(userDetail -> {
//                userCardService.update(Wrappers.lambdaUpdate(UserCard.class)
//                        .set(UserCard::getBankNo, bankCardRequestDto.getBankNo())
//                        .set(UserCard::getBankTitle, bankCardRequestDto.getBankTitle())
//                        .eq(UserCard::getUserId, userDetail.getId())
//                );
//            }, executor);
//
//            // 等待所有任务完成
//            CompletableFuture.allOf(task1, task3).join();
//            log.info("任务已经完成，listener收到消息========================================");
//
//        } catch (Exception e) {
//            // 处理异常
//            log.error("处理绑卡消息失败", e);
//            throw new RuntimeException("处理绑卡消息失败", e);
//        } finally {
//            // 关闭线程池
//            executor.shutdown();
//        }
        //1. 根据请求流水号，更新用户表u_user_detail中绑卡状态
        userDetailService.update(Wrappers.lambdaUpdate(UserDetail.class)
                .set(UserDetail::isCardBindStatus,true)
                .eq(UserDetail::getReqCode,bankCardRequestDto.getReqCode())
        );

        //2. 根据用户编码查询用户id
        UserDetail userDetail = userDetailService.getOne(
                Wrappers.lambdaQuery(UserDetail.class)
                        .select(UserDetail::getId)
                        .eq(UserDetail::getUserCode, bankCardRequestDto.getUserCode()));

        //3. 根据用户id修改u_user_card绑卡记录表中信息
        userCardService.update(Wrappers.lambdaUpdate(UserCard.class)
                .set(UserCard::getBankNo,bankCardRequestDto.getBankNo())
                .set(UserCard::getBankTitle,bankCardRequestDto.getBankTitle())
                .eq(UserCard::getUserId,userDetail.getId())
        );
    }
}
